from db import Db
import math

class Page:
    #total 总页数  page当前页  num 一页显示几条数据
    def __init__(self,total=0,page=1,num=1):
        self.total = int(total)
        self.page =int(page)
        self.num = int(num)
        if self.page <=0:
            self.page = 1
    #计算页数
    def page_num(self):
        # if self.total % self.num == 0:
        #     return self.total // self.num
        # else:
        #     return self.total // self.num+1
        return math.ceil(self.total/self.num)
    
    #获取分页后数据
    def get_data(self,sql):
        #limit OFFSET (page-1)*num ,num 
        _limit = (self.page-1)*self.num
        _offset = self.num
        sql +=" limit %s,%s"%(_limit,_offset)
        #执行sql
        mydb = Db()
        res = mydb.get_all(sql)
        return res

